package com.level2app.view.dialog;

import android.content.Context;
import android.graphics.Paint;
import android.view.Gravity;
import android.view.WindowManager;

import com.level2app.R;
import com.level2app.databinding.DialogDateBinding;
import com.level2app.utils.DateUtil;
import com.level2app.utils.ScreenUtil;
import com.level2app.utils.ToastHelper;
import com.level2app.view.calendar.MyCalendarListView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/*
 * Author: S
 * Date: 2024/9/7
 * 选择日历弹窗
 */
public class DateDialog extends BaseCommonBottomDialog<DialogDateBinding> {

    private final Context context;
    private final OnClickListener listener;
    private String mStartDate, mEndDate;

    public DateDialog(Context context, OnClickListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void initView() {
        WindowManager.LayoutParams params = Objects.requireNonNull(getWindow()).getAttributes();
        int height = ScreenUtil.getMaxHeight(getContext()) - ScreenUtil.getStatusBarHeight(getContext()) - 8;
        params.height = height; // 或设置具体的像素值
        getWindow().setAttributes(params);

        mBinding.clearTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mBinding.clearTv.getPaint().setAntiAlias(true);

        mBinding.closeIv.setOnClickListener(o -> dismiss());

        mBinding.clearTv.setOnClickListener(o -> mBinding.calendarList.clearDate());
        mBinding.nextTv.setOnClickListener(o -> {
            if (mEndDate == null) {
                ToastHelper.showToast(context.getString(R.string.select_end_date));
                return;
            }
            if (listener != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                try {
                    Date sDate = sdf.parse(mStartDate);
                    Date eDate = sdf.parse(mEndDate);
                    listener.onChooseListener(sDate, eDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            dismiss();
        });
        mBinding.calendarList.setOnDateSelected(new MyCalendarListView.OnDateSelected() {
            @Override
            public void selected(String startDate, String endDate) {
                mStartDate = startDate;
                mEndDate = endDate;
            }

            @Override
            public void hasSelect(boolean select) {

            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_date;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

//    @Override
//    protected int getAnimations() {
//        return R.style.dialog_no_anim;
//    }

    public interface OnClickListener {
        void onChooseListener(Date startDate, Date endDate);
    }
}